package com.example.week12_networkprogramming;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by HP on 2022/5/22.
 */

public class DBUti {

    private static String user = "root";
    private static String password = "mypassword";

    private static Connection getConn(String dbName) {

        Connection connection = null;

        try {

            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/" + dbName;
            connection = DriverManager.getConnection(url, user, password);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static ResultSet queryReader(String reader_num) {

        ResultSet rs = null;

        Connection connection = getConn("library");

        if (connection != null) {
            try {
                PreparedStatement ps;
                Statement smt = connection.createStatement();
                String sql = "select*fromreaderswherereader_numberLIKE'%" + reader_num + "%'";
                rs = smt.executeQuery(sql);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return rs;
    }
}
